import { Body, Controller, Post, Req, UseGuards, UseInterceptors } from "@nestjs/common";
import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiTags, ApiExtraModels } from "@nestjs/swagger";


import { UserService } from "./user.service";

import { CreateUserDto } from "./dto/create-user.dto";
import { CreateTokenDto } from "./dto/create-token.dto";
import { ResultData } from "@/common/utils/result";
import { User } from "@/modules/user/entities/user.entity";
import { AllowAnon } from "@/decorators/allow-anon.decorator";
import { LoginUser } from "@/modules/user/dto/login-user.dto";
import { LogInterceptor } from "@/interceptors/log.inerceptor";

@ApiTags("登录注册")
@ApiExtraModels(ResultData, User, CreateTokenDto)
@Controller()
@UseInterceptors(LogInterceptor)
export class BaseController {
	constructor(private readonly userService: UserService) {
	}
	
	// @Post("register")
	// @ApiOperation({ summary: "用户注册" })
	// @AllowAnon()
	// async create(@Body() user: CreateUserDto): Promise<ResultData> {
	// 	return await this.userService.create(user);
	// }
	
	@Post("login")
	@ApiOperation({ summary: "登录" })
	@AllowAnon()
	async login(@Body() dto: LoginUser): Promise<ResultData> {
		return await this.userService.login(dto.username, dto.password);
	}
	
	@Post("update/token")
	@ApiOperation({ summary: "刷新token" })
	@ApiBearerAuth()
	async updateToken(@Req() req): Promise<ResultData> {
		return await this.userService.updateToken(req.user.id);
	}
}
